<?php
$txt=file_get_contents('php://input');

if (!class_exists("go_sql")){
include("/var/www/html/new/conf/le.je");
  $le=new go_sql();
  $le->connect();
}
$ac1='16ucom';
$pw1='c33367701511b4f6020ec61ded352059';
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac1|pw_16u=$pw|auth_16u=true"));

$s1=new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));
$ac='123545'; //百度糯米接口账号
$pw='fa23a74898ad0ca49b6a9ae82e9b3fb8';

/*$txt='custId=123545&encode=2b7ca9af3c552641d57360469794d0a9&time=1404701600508&productId=4249&linkman=%5B%7B%22link_man%22%3A%22tianyongqian%22%2C%22link_phone%22%3A%2213811135499%22%2C%22link_email%22%3A%22tianyongqian%40baidu.com%22%7D%5D&thirdOrderId=1&thirdOrderNum=1&thirdTicketId=1';
*/
parse_str($txt,$date); //转换数据
//1228348800
  

$custId = '123545'; //二维码商帐号
$p_encode = '123545'; //二维码商密钥
$time = $date['time']; //获取时间戳
$encode = md5($p_encode.$time.$custId); //加密签名

if($date['encode'] == $encode) //判断签名是否相等
{
	$tid = $date['productId'];  //门票id
    $get_id=$s1->__soapCall("Get_Ticket_List",array("ac"=>$ac,"pw"=>$pw,"in0"=>"","in1"=>$tid ));
	$get_result=simplexml_load_string($get_id);
	$lid = (int)$get_result->Rec->UUlid;  //景区id
	$landid = (int)$get_result->Rec->UUaid;	//供应商id
	$UUremotenum = $date['thirdOrderId'].'_'.$date['thirdTicketId']; //远端订单号  注：是thirdOrderId 和 thirdTicketId的合并，回调的时候会用到
	$price = 100;    // 价格
	$num = $date['thirdOrderNum']; //数量
	if(strlen($date['time'])>10) $playtime=date('Y-m-d',substr($date['time'],0,10)); //游玩日期
	$aa1 = json_decode($date['linkman']);
	$name = $aa1[0]->link_man; //游客姓名
	$tel = $aa1[0]->link_phone; //游客手机
	//这里是下单程序
	$send_array =array("ac"=>$ac,"pw"=>$pw,"in0"=>$lid,"in1"=>$tid,"in2"=>$UUremotenum,"in3"=>$price,"in4"=>1,"in5"=>$playtime,"in6"=>'百度糯米',"in7"=>$tel,"in8"=>"","in9"=>0,"in10"=>2,"in11"=>0,"in12"=>"","in13"=>"","in14"=>0,"in15"=>0,"in16"=>$landid);
	
	$xml=$s1->__soapCall("PFT_Order_Submit",$send_array);
	$result=simpleXML_load_string($xml);
	try
 	{
		if($result->Rec->UUerrorcode){
			if((int)$result->Rec->UUerrorcode==122){
			//授信不足，提醒供应商给分销商授信
				$str="select mobile from pft_member where id='$landid' limit 1";	
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$aid_Tel=$GLOBALS['le']->f('mobile');
				if($aid_Tel){
					$s->Send_SMS_V($aid_Tel,"系统公告:您的分销商[百度糯米网]因授信不足下单失败，失败订单号:".$UUremotenum.",请尽快补充授信后联系OTA商家补单，避免影响销量");
				}
			}elseif((int)$result->Rec->UUerrorcode==1075){ 	
				echo '{"msg":"success", "data":{"orderId":"'.$result->Rec->UUordernum.'"}}';
				exit;
			}
			$txt_json=json_encode($send_array);  
			$txt_json.='|'.$result->Rec->UUerrorcode;
			logfile('-------'.date('Y-m-d H:i:s').'-------','nm_debug.txt');
			logfile($txt,'nm_debug.txt');
			logfile($txt_json,'nm_debug.txt');
			echo '{"msg":"failed", "data":{"orderId":""}}';
			throw new Exception(json_encode('{"msg":"failed", "data":{"orderId":""}}'));				
		}
		else{
		//成功
			echo '{"msg":"success", "data":{"orderId":"'.$result->Rec->UUordernum.'"}}';
		}
	}
	catch (Exception $e)
	{
		echo $e->getMessage();
	}
}
function err($txt,$path){
	$fp = fopen($path,"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}
function logfile($txt,$path){
	$fp = fopen($path,"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
} 
?>